package com.itheima.controller;

import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBen;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/checkgroup")
public class CheckGroupController {
    @Autowired
    private CheckGroupService checkGroupService;
    @PostMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup, @RequestParam List<Integer> checkitemIds){
        checkGroupService.add(checkGroup,checkitemIds);
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }
    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBen queryPageBean){
        PageResult<CheckGroup> pageResult = checkGroupService.findPage(queryPageBean);
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,pageResult);
    }
@GetMapping("/{id}")
    public Result findById(@PathVariable int id){
      CheckGroup checkGroup= checkGroupService.findById(id);
         return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroup);
}


    @GetMapping("/findCheckItemIdsByCheckGroupId/{id}")
    public Result findCheckItemIdsByCheckGroupId(@PathVariable int id){
        List<Integer> checkitemIds = checkGroupService.findCheckItemIdsByCheckGroupId(id);
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkitemIds);
    }


    @PutMapping("/update")
    public Result update(@RequestBody CheckGroup checkGroup, @RequestParam List<Integer> checkitemIds){
        checkGroupService.update(checkGroup, checkitemIds);
        return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable int id ){
        checkGroupService.deleteByid(id);
        return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS );
    }

    @GetMapping("/findAll")
    public Result findAll(){
       List<CheckGroup> checkGroups= checkGroupService.findAll();
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroups );
    }
}
